package com.soul.shuiyou;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.soul.utils.EncryptUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

public class ShuiYouInvoke {

    public static void main(String[] args) {

        suiYouInvoke();

    }

    private static void suiYouInvoke() {
        try {
            String appId = "jincheng";
            String secretKey = "123456";
            String aesKey = "Syu0JdkYvyuu9930";
            String taxId = "12440100G341146862";
            String authId = "9112011632857960XM";
            long timeNow = new Date().getTime();
            System.out.println("timeNow=" + timeNow);
            HashMap<String, String> headerMap = new HashMap<>();
            headerMap.put("appid", appId);
            headerMap.put("timestamp", String.format("%d", timeNow));
            //sign需要md5加密 appid=000000&key=secretkey&timestamp=1615452174153
            String sign = EncryptUtil.md5(String.format("appid=%s&key=%s&timestamp=%d",
                    appId, secretKey, timeNow));
            System.out.println("sign=" + sign);
            headerMap.put("sign", sign);


            HashMap<String, Object> paramMap = new HashMap<>();
            String uuid = UUID.randomUUID().toString();
            System.out.println("uuid=" + uuid);
            paramMap.put("jylsh", uuid);
            paramMap.put("nsrsbh", taxId);
            paramMap.put("authId", authId);
            paramMap.put("authType", "");
            paramMap.put("authSource", "");
            paramMap.put("authTimeBegin", "2022-08-10");
            paramMap.put("authTimeEnd", "2023-08-15");
            System.out.println("paramMap=" + JSONObject.toJSONString(paramMap));
            //String dataEncrypt = AESUtils.aes256ECBPkcs7PaddingEncrypt(JSONObject.toJSONString(paramMap), aesKey);
            String dataEncrypt = EncryptUtil.aesECBPkcs5Encrypt(JSONObject.toJSONString(paramMap), aesKey);
            System.out.println("data=" + dataEncrypt);

            HashMap<String, Object> encryptParamMap = new HashMap<>();
            encryptParamMap.put("data", dataEncrypt);


            String result = HttpUtil.createPost(String.format("https://ts1.171win.com.cn/%s/api/client/taxInfo",
                    appId))
                    .contentType("application/json")
                    .headerMap(headerMap, false)
                    .body(JSONObject.toJSONString(encryptParamMap))
                    .execute().body();
            System.out.println("result=" + result);
            JSONObject resultJson = JSONObject.parseObject(result);
            boolean success = (boolean) resultJson.get("success");
            System.out.println("success=" + success);
            String value = (String) resultJson.get("value");
            //System.out.println("value=" + value);
            String dataDecrypt = EncryptUtil.aesECBPkcs5Decrypt(value, aesKey);
            //System.out.println("dataDecrypt=" + dataDecrypt);
            JSONObject valueJson = JSONObject.parseObject(dataDecrypt);

            JSONArray qyxxList = (JSONArray)valueJson.get("qyxxList");
            System.out.println("qyxxList="+qyxxList.get(0));

            JSONArray kphzxxList = (JSONArray)valueJson.get("kphzxxList");
            System.out.println("kphzxxList="+kphzxxList.get(0));

            JSONArray syKphzxxList = (JSONArray)valueJson.get("syKphzxxList");
            System.out.println("syKphzxxList="+syKphzxxList.get(0));

            JSONArray xyKphzxxList = (JSONArray)valueJson.get("xyKphzxxList");
            System.out.println("xyKphzxxList="+xyKphzxxList.get(0));

            JSONArray khxsdqxxList = (JSONArray)valueJson.get("khxsdqxxList");
            System.out.println("khxsdqxxList="+khxsdqxxList.get(0));

            JSONArray spxsxxList = (JSONArray)valueJson.get("spxsxxList");
            System.out.println("spxsxxList="+spxsxxList.get(0));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
